import { defineStore } from 'pinia'

// 定义菜单数据存储
export const useMenuStore = defineStore('menu', {
  state: () => ({
    // 菜单数据
    menuData: [
      {
        "id": "t5",
        "name": "乾坤测试.",
        "open": true,
        "backImage": "icon-f071",
        "leafNodes": [
          {
            "attr1": "0",
            "attr6": "0",
            "backImage": "icon-f213",
            "favorite": false,
            "id": "11980911107215527579391",
            "isLeaf": false,
            "leafNodes": [],
            "name": "子应用测试菜单1",
            "nodeLevel": 3,
            "open": false,
            "pid": "1198091110721552757939",
            "showIndex": "1"
          },
          {
            "attr1": "0",
            "attr6": "0",
            "backImage": "icon-f213",
            "favorite": false,
            "id": "11980911107215527579491",
            "isLeaf": false,
            "leafNodes": [],
            "name": "子应用测试-无界",
            "nodeLevel": 3,
            "open": false,
            "pid": "1198091110721552757939",
            "showIndex": "2"
          }
        ]
      },
      {
        "id": "t4",
        "name": "帮助中心",
        "open": true,
        "backImage": "icon-f078",
        "leafNodes": [
          {
            "attr1": "0",
            "attr6": "0",
            "backImage": "icon-f213",
            "favorite": false,
            "id": "11980911107215527579094",
            "isLeaf": false,
            "leafNodes": [],
            "name": "我的通知",
            "nodeLevel": 3,
            "open": false,
            "pid": "1198091110721552757909",
            "showIndex": "4",
            "viewType": "C",
            "url": "business/notice/NoticeInfoQuery"
          },
          {
            "attr1": "0",
            "attr6": "0",
            "backImage": "icon-f213",
            "favorite": false,
            "id": "119809111072155275790941",
            "isLeaf": false,
            "leafNodes": [],
            "name": "我的通知2",
            "nodeLevel": 3,
            "open": false,
            "pid": "1198091110721552757988",
            "showIndex": "4",
            "viewType": "C",
            "url": "business/notice/NoticeInfoQuery2"
          },
          {
            "attr1": "0",
            "attr6": "1",
            "backImage": "icon-f213",
            "favorite": false,
            "id": "1512353731590280963679",
            "isLeaf": false,
            "leafNodes": [],
            "name": "帮助文档",
            "nodeLevel": 3,
            "open": false,
            "pid": "1198091110721552757908",
            "showIndex": "1",
            "viewType": "C",
            "url": "help/help_doc/HelpDocMain"
          }
        ]
      },
      {
        "id": "t1",
        "name": "基础信息",
        "open": false,
        "backImage": "icon-f074",
        "leafNodes": [
          {
            "attr1": "0",
            "attr6": "0",
            "backImage": "icon-f213",
            "favorite": false,
            "id": "1512353982623926633670",
            "isLeaf": false,
            "leafNodes": [],
            "name": "成本中心",
            "nodeLevel": 3,
            "open": false,
            "pid": "1198091110721552757908",
            "showIndex": "4"
          },
          {
            "attr1": "0",
            "attr6": "0",
            "backImage": "icon-f213",
            "favorite": false,
            "id": "1512353982623926633671",
            "isLeaf": false,
            "leafNodes": [],
            "name": "利润中心",
            "nodeLevel": 3,
            "open": false,
            "pid": "1198091110721552757908",
            "showIndex": "4"
          },
          {
            "attr1": "0",
            "attr6": "0",
            "backImage": "icon-f213",
            "favorite": false,
            "id": "11980911107215527579068",
            "isLeaf": false,
            "leafNodes": [],
            "name": "系统日志查询",
            "nodeLevel": 3,
            "open": false,
            "pid": "1198091110721552757908",
            "showIndex": "22"
          },
          {
            "attr1": "0",
            "attr6": "0",
            "backImage": "icon-f213",
            "favorite": false,
            "id": "11980911107215527579086",
            "isLeaf": false,
            "leafNodes": [],
            "name": "系统日历表",
            "nodeLevel": 3,
            "open": false,
            "pid": "1198091110721552757908",
            "showIndex": "22"
          },
          {
            "attr1": "0",
            "attr6": "0",
            "backImage": "icon-f213",
            "favorite": false,
            "id": "11980911107715527579082",
            "isLeaf": false,
            "leafNodes": [],
            "name": "账套维护",
            "nodeLevel": 3,
            "open": false,
            "pid": "1198091110721552757908",
            "showIndex": "18",
            "viewType": "C"
          },
          {
            "attr1": "1",
            "attr6": "0",
            "backImage": "icon-f213",
            "favorite": false,
            "id": "1513353889426781510669",
            "isLeaf": false,
            "leafNodes": [],
            "name": "子应用维护",
            "nodeLevel": 3,
            "open": false,
            "pid": "1198091110721552757908",
            "viewType": "C",
            "showIndex": "1",
            "url": "sagacity/app_code/AppCodeMain"
          },
          {
            "attr1": "0",
            "attr6": "0",
            "backImage": "icon-f213",
            "favorite": false,
            "id": "11980911107215527579082",
            "isLeaf": false,
            "leafNodes": [],
            "name": "系统常量维护",
            "nodeLevel": 3,
            "open": false,
            "pid": "1198091110721552757908",
            "showIndex": "18",
            "viewType": "C",
            "url": "sagacity/system_constant/SystemConstantMain"
          },
          {
            "attr1": "0",
            "attr6": "0",
            "backImage": "icon-f213",
            "favorite": false,
            "id": "1512353952428103422669",
            "isLeaf": false,
            "leafNodes": [],
            "name": "机构信息管理",
            "nodeLevel": 3,
            "open": false,
            "pid": "1198091110721552757908",
            "viewType": "C",
            "showIndex": "2",
            "url": "system/organ_info/OrganInfoMain"
          },
          {
            "attr1": "0",
            "attr6": "0",
            "backImage": "icon-f213",
            "favorite": false,
            "id": "1512353982623926633669",
            "isLeaf": false,
            "leafNodes": [],
            "nodeLevel": 3,
            "open": false,
            "viewType": "C",
            "pid": "1198091110721552757908",
            "showIndex": "4"
          },
          {
            "attr1": "1",
            "attr6": "0",
            "backImage": "icon-f213",
            "favorite": true,
            "id": "1512353889426781510669",
            "isLeaf": false,
            "name": "字典类别定义",
            "nodeLevel": 3,
            "open": false,
            "pid": "1198091110721552757908",
            "viewType": "C",
            "showIndex": "1",
            "url": "sagacity/dict_type/DictTypeMain"
          },
          {
            "attr1": "0",
            "attr6": "0",
            "backImage": "icon-f213",
            "favorite": false,
            "id": "11980911107215527579084",
            "isLeaf": false,
            "leafNodes": [],
            "name": "字典数据维护",
            "nodeLevel": 3,
            "open": false,
            "pid": "1198091110721552757908",
            "viewType": "C",
            "showIndex": "20",
            "url": "sagacity/dict_detail/DictDetailMain"
          },
          {
            "attr1": "0",
            "attr6": "0",
            "backImage": "icon-f213",
            "favorite": false,
            "id": "1560252085764991249225",
            "isLeaf": false,
            "leafNodes": [],
            "name": "文件上传",
            "nodeLevel": 3,
            "open": false,
            "pid": "1198091110721552757908",
            "showIndex": "28",
            "viewType": "C",
            "url": "common/file_upload/FileUploadMain.vue"
          }
        ]
      },
      {
        "id": "t0",
        "name": "DEMO测试",
        "open": false,
        "backImage": "icon-f093",
        "leafNodes": [
          {
            "attr1": "0",
            "attr6": "0",
            "backImage": "icon-f213",
            "favorite": false,
            "id": "demo-1",
            "isLeaf": false,
            "leafNodes": [],
            "name": "富文本编辑器",
            "viewType": "C",
            "nodeLevel": 3,
            "open": false,
            "pid": "1198091110721552757908",
            "showIndex": "28",
            "url": "z-demos/demos/RichTextDemo"
          },
          {
            "attr1": "0",
            "attr6": "0",
            "backImage": "icon-f213",
            "favorite": false,
            "id": "demo-2",
            "isLeaf": false,
            "leafNodes": [],
            "viewType": "C",
            "nodeLevel": 3,
            "open": false,
            "pid": "1198091110721552757908",
            "showIndex": "28",
            "url": "z-demos/demos/RichTextDemo"
          },
          {
            "attr1": "0",
            "attr6": "0",
            "backImage": "icon-f213",
            "favorite": false,
            "id": "demo-3",
            "isLeaf": false,
            "leafNodes": [],
            "name": "序列分割器",
            "viewType": "C",
            "nodeLevel": 3,
            "open": false,
            "pid": "1198091110721552757908",
            "showIndex": "28"
          },
          {
            "attr1": "0",
            "attr6": "0",
            "backImage": "icon-f213",
            "favorite": false,
            "id": "demo-4",
            "isLeaf": false,
            "leafNodes": [],
            "name": "地区选择器",
            "viewType": "C",
            "nodeLevel": 3,
            "open": false,
            "pid": "1198091110721552757908",
            "showIndex": "28"
          },
          {
            "attr1": "0",
            "attr6": "0",
            "backImage": "icon-f213",
            "favorite": false,
            "id": "1560252085764992249225",
            "isLeaf": false,
            "leafNodes": [],
            "viewType": "C",
            "nodeLevel": 3,
            "open": false,
            "pid": "1198091110721552757908",
            "showIndex": "28"
          }
        ]
      }
    ]
  }),

  // 计算属性
  getters: {
    // 获取所有菜单项（一维数组）
    allMenuItems(state) {
      const items = []

      const extractItems = (nodes) => {
        if (!nodes) return

        nodes.forEach(node => {
          items.push(node)

          if (node.leafNodes && node.leafNodes.length) {
            extractItems(node.leafNodes)
          }
        })
      }

      extractItems(state.menuData)
      return items
    },

    // 根据ID查找菜单项
    getMenuItemById: (state) => (id) => {
      const findItem = (nodes) => {
        if (!nodes) return null

        for (const item of nodes) {
          if (item.id === id) return item

          if (item.leafNodes && item.leafNodes.length) {
            const found = findItem(item.leafNodes)
            if (found) return found
          }
        }

        return null
      }

      return findItem(state.menuData)
    }
  },

  // 操作方法
  actions: {
    // 如需要，可以添加修改菜单数据的方法
    updateMenuItem(id, updates) {
      const item = this.getMenuItemById(id)
      if (item) {
        Object.assign(item, updates)
      }
    }
  }
})
